	$(function() {	 
		//Banner图片加载
		readBannerList();
	});

	var baseUrl = "";
	
	//产品分类加载
	function readBannerList(){
		$.ajax({
			type: "POST",
			dataType: "JSON",
			url:baseUrl+'bannerController/selectBannerListrAll',
			contentType:'application/json;charset=UTF-8',
			success: function (result) {
				if(result!=null){
					var str = "";
					var checked = "";
					for(var i=0;i<result.length;i++){
						str+="<li data_id="+result[i].id+">";
						str+="<div class=\"control-group\">";
						str+="<div class=\"img_tag\">";
						str+="<img src=\""+isNull(result[i].imgurl)+"\" />";
						str+="</div>";
						str+="</div>";
						str+="<div class=\"control-group\">";
						str+="<label class=\"control-label\" style=\"padding-top:12px;margin-left:20px\" for=\"topnavnum\">";
						str+="<a class=\"btn btn-info filelink\" herf=\"javascript:void(0);\">";
						str+="<i class=\"icon-cloud-upload\"></i> 上传图片";
						str+="<input type=\"file\" multiple=\"multiple\" name=\"image\" class=\"readsctp\"  id=\"btn1_uploadimg"+i+"\">";
						str+="</a>";
						str+="</label>";
						str+="<div class=\"controls ad-config\">";
						str+="<div class=\"input-prepend\"><span class=\"add-on\">网址</span>";
						str+="<input type=\"text\" name=\"url[16]\" value=\""+isNull(result[i].url)+"\" class=\"input200\" value=\"\"  id=\"url"+i+"\"/>";
						str+="</div>";
						str+="<div class=\"input-prepend\"> <span class=\"add-on\">序号</span>";
						str+="<input type=\"text\" name=\"listorder[16]\" id=\"listorder\" value=\""+isNull(result[i].sort)+"\" class=\"input30\" value=\"\" id=\"sort"+i+"\"/>";
						str+="</div>";
						str+="<div class=\"input-prepend\" style=\"line-height:30px\"><label>显示</label>";
						if(isNull(result[i].indexif)!=""){
							if(isNull(result[i].indexif)==1){
								checked = "checked";
							}else{
								checked = "";
							}
						}
						str+="<input type=\"checkbox\" "+checked+" name=\"status[16]\" value=\"1\" class=\"input40\" id=\"indexif"+i+"\"></div>";
						str+="<div class=\"input-prepend\">";
						str+="<a class=\"del btn btn-danger btn-mini btn-nobr nomargin J_ajax_del\" href=\"javascript:;\">删除</a>";
						str+="</div>";
						str+="</div>";
						str+="</div>";
						str+="</li>";
					}
					$("#banner_table_list").html(str); 
					//图片
					var imgAddress = "";
					$('.readsctp').on("change", function () {
						 singleImg($(this));
					      $(this).parent().parent().parent('.control-group').siblings('div').children('.img_tag').html('<img src="'+urls+'" />');
					      var id = $(this).parent().parent().parent().parent().attr('data_id');
					      //上传图片到文件夹
					      var formData=new FormData();
					      var imgId = $(this).attr("id");
					      formData.append("img",$("#"+imgId+"")[0].files[0]);
					      formData.append("type", 'banner');//也可以传递其他字段
					      $.ajax({
					          type:"post",
					          url:baseUrl+"productController/imgUpload",
					          data:formData,
					          async:false, 
					          contentType: false,
					          processData: false,
					          dataType:"json",
					          success:function(result){
					          	if(result.errno=="0"){  //图片上传成功
					          		imgAddress = result.data[0];
					          	}
					          },
					          error:function (result) {
					          }
					      })
					      var update = returnUpdate();	//获取当前时间
						  var data = {
							id:		id,
							imgurl:	imgAddress,
							update:		update
					  	  }
						 $.ajax({
							url:baseUrl+'/bannerController/insertUpdateBanner',
							data : data,
							type : 'POST',
							dataType: "JSON",
							success : function(result) {
								if (result.state=="200") {
									
								}
							},
							error : function(error) {
								alert("获取产品内容列表失败！");
							}
						 });
				      });
					
				    //网址
				    $('.input200').on("change", function () {
				        var id = $(this).parent().parent().parent().parent().attr('data_id');
				        var url = $(this).val();
				        var update = returnUpdate();	//获取当前时间
				  	  	var data = {
					  		id:		id,
					  		url:	url,
					  		update:		update
				    	}
				  	 $.ajax({
				  		url:baseUrl+'/bannerController/insertUpdateBanner',
				  		data : data,
				  		type : 'POST',
				  		dataType: "JSON",
				  		success : function(result) {
				  			if (result.state=="200") {
				  				
				  			}
				  		},
				  		error : function(error) {
				  			alert("获取产品内容列表失败！");
				  		}
				  	 });
				  });
				    
				    //序号
				    $('.input30').on("change", function () {
				        var id = $(this).parent().parent().parent().parent().attr('data_id');
				        var sort = $(this).val();
				        var imgId = $(this).attr("id");
				        var update = returnUpdate();	//获取当前时间
				  	  	var data = {
					  		id:		id,
					  		update:		update,
					  		sort:		sort
				    	}
				  	 $.ajax({
				  		url:baseUrl+'/bannerController/insertUpdateBanner',
				  		data : data,
				  		type : 'POST',
				  		dataType: "JSON",
				  		success : function(result) {
				  			if (result.state=="200") {
				  				
				  			}
				  		},
				  		error : function(error) {
				  			alert("获取产品内容列表失败！");
				  		}
				  	 });
				  });
				    
				    //显示
				    $('.input40').on("change", function () {
				        var id = $(this).parent().parent().parent().parent().attr('data_id');
				        var indexif = 1;
				        //判断checkbox是否被选中  
				        if($(this).is(":checked")){
				    		indexif = 1;
				        }  
				        else{
				        	indexif = 2;
				        }
				        var imgId = $(this).attr("id");
				        var update = returnUpdate();	//获取当前时间
				  	  	var data = {
					  		id:		id,
					  		update:		update,
					  		indexif:	indexif,
				    	}
				  	 $.ajax({
				  		url:baseUrl+'/bannerController/insertUpdateBanner',
				  		data : data,
				  		type : 'POST',
				  		dataType: "JSON",
				  		success : function(result) {
				  			if (result.state=="200") {}
				  		},
				  		error : function(error) {
				  			alert("获取产品内容列表失败！");
				  		}
				  	 });
				  });
					
					
				}else{
					$("#banner_table_list").html("暂无数据");
				}
			},
			error: function (result) {
			}
		});
	}
	
	var urls = null;
	  function singleImg(obj) {
	    for(var i=0;i<obj[0].files.length;i++){ 
	      if(window.createObjectURL != undefined) { // basic
	    	  urls = window.createObjectURL(obj[0].files[i]);
	      } else if(window.URL != undefined) { // mozilla(firefox)
	    	  urls = window.URL.createObjectURL(obj[0].files[i]);
	      } else if(window.webkitURL != undefined) { // webkit or chrome
	    	  urls = window.webkitURL.createObjectURL(obj[0].files[i]);
	      }

	      if (urls == null) return false;

	      obj.siblings('input[type="text"]').val(urls);
	      obj.siblings('img').src = urls;
	    }
	  }
	

	//判断为数据库null
	function isNull(reason){
		if(reason!=null&&reason!=""&&reason!="null"){
			return reason;
		}else{
			return "";
		}
	}
	

		
		//返回当前时间方法  格式 xxxx-xx-xx xx:xx:xx
		function returnUpdate(){
			var date=new Date();   
			var year=date.getFullYear(); //获取当前年份   
			var mon=date.getMonth()+1; //获取当前月份   
			var da=date.getDate(); //获取当前日   
			var h=date.getHours(); //获取小时   
			var m=date.getMinutes(); //获取分钟   
			var s=date.getSeconds(); //获取秒   
			if(mon.toString().length==1){
				mon="0"+mon;
			}
			if(da.toString().length==1){
				da="0"+da;
			}
			if(h.toString().length==1){
				h="0"+h;
			}
			if(m.toString().length==1){
				m="0"+m;
			}
			if(s.toString().length==1){
				s="0"+s;
			}
			return year+'-'+mon+'-'+da+' '+h+':'+m+':'+s; 
		}
		
		
		
		
		
	    
		
		
		
		
		
		
		
		
